name: Prebuild TDLib for WASM
on:
  workflow_dispatch:
    inputs:
      tdlib:
        # https://github.com/tdlib/td/search?q=Update+version+to&type=commits
        # bbe37ee594d97f3c7820dd23ebcd9c9b8dac51a0 - 1.8.8
        description: "TDLib git ref (e.g. v1.8.0 or a commit hash)"
        type: string
        required: true

jobs:
  build-wasm:
    name: Build TDLib for Web
    runs-on: ubuntu-22.04
    steps:
      - name: Install gperf
        run: sudo apt-get install -y -q gperf clang-14 libc++-dev libc++abi-dev tar wget
      - name: Install latest CMake
        run: |
          wget https://github.com/Kitware/CMake/releases/download/v3.25.0/cmake-3.25.0-linux-x86_64.sh 
          chmod +x ./cmake-*.sh
          mkdir cmake
          ./cmake-*.sh --prefix=./cmake --skip-license
          sudo cp -RTv ./cmake /usr/
          cmake --version
      - uses: actions/checkout@v3
        with:
          repository: "tdlib/td"
          ref: ${{ inputs.tdlib }}

      - name: Build for WASM
        run: |
          git clone https://github.com/emscripten-core/emsdk.git
          cd emsdk
          git pull
          ./emsdk install 3.1.1
          ./emsdk activate 3.1.1
          source ./emsdk_env.sh
          cd ..

          cd example/web
          ./build-openssl.sh
          ./build-tdlib.sh
          ./copy-tdlib.sh
      - uses: actions/upload-artifact@v3
        with:
          name: tdlib-js
          path: example/web/tdweb/src/prebuilt/release/*
